package AccionBoton;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

import paginas.MenuUsuarioDatosTab;
import util.ImageFileView;
import util.ImageFilter;
import util.ImagePreview;

import com.sun.java.swing.plaf.nimbus.LoweredBorder;

public class AccionSeleccionImagen implements ActionListener{
	
	JFileChooser fc;
	MenuUsuarioDatosTab _panel;
	
	public AccionSeleccionImagen(MenuUsuarioDatosTab panel){
		
		_panel = panel;
		
	}
	
    public void actionPerformed(ActionEvent e) {
        //Set up the file chooser.
        if (fc == null) {
            fc = new JFileChooser();

	    //Add a custom file filter and disable the default
	    //(Accept All) file filter.
            fc.addChoosableFileFilter(new ImageFilter());
            fc.setAcceptAllFileFilterUsed(false);

	    //Add custom icons for file types.
            fc.setFileView(new ImageFileView());

	    //Add the preview pane.
            fc.setAccessory(new ImagePreview(fc));
        }

        //Show it.
        int returnVal = fc.showDialog(_panel,"Abrir");

        //Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            _panel.setImagen(file);
			JLabel foto = _panel.getImagenMostrada();
			foto.setBorder(new LoweredBorder());
			ImageIcon im = new ImageIcon(_panel.getImagen().getPath());
			int alto = im.getIconHeight();
			double escala = (double)alto/125;
			int ancho = (int)(im.getIconWidth()/escala);
			Image redimensionada = im.getImage().getScaledInstance(ancho,
					125, Image.SCALE_DEFAULT);
			foto.setIcon(new ImageIcon(redimensionada));
			foto.setPreferredSize(new Dimension(foto.getIcon().getIconWidth()+15,foto.getIcon().getIconHeight()+15));

			_panel.setImagenMostrada(foto);
			_panel.getImagenMostrada().setPreferredSize(new Dimension(foto.getIcon().getIconWidth()+15,foto.getIcon().getIconHeight()+15));

			_panel.getPanel3().setVisible(false);
			_panel.getPanel3().setVisible(true);

        } else {
          
        }
       

        //Reset the file chooser for the next time it's shown.
        fc.setSelectedFile(null);
    }


}
